﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Menus.Model;

namespace Menus
{
    public partial class fmPlatDetail : Form
    {
        AutoCompleteStringCollection colValues = new AutoCompleteStringCollection();
        
        private LoadGridViewDelegate loadGridView;
        private int m_idPlat = -1;

        public fmPlatDetail(LoadGridViewDelegate callback, int idPlat = -1)
        {
            InitializeComponent();
            loadGridView = callback;
            lstIngredient.Items.Clear();

            //initialisation du textBox autocompletion
            txtIngredientName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtIngredientName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtIngredientName.AutoCompleteCustomSource = colValues;
            m_idPlat = idPlat;
            LoadForm();
        }

        /// <summary> LoadForm()
        /// chargement de la form
        /// </summary>
        private void LoadForm()
        {
            txtIngredientName.Clear();
            txtNomPlat.Clear();
            lstIngredient.Items.Clear();
            InitAutoCompletionMode();
            txtNomPlat.Focus();

            //si on est en mode modification, on a le idPlat initialisé
            if (m_idPlat != -1)
            {
                PlatManager manager = new PlatManager();
                var plat = manager.load(m_idPlat);
                txtNomPlat.Text = plat.Libelle;
                foreach (var ing in plat.Ingredient)
                {
                    lstIngredient.Items.Add(ing.Libelle);
                }
            }

        }

        private void InitAutoCompletionMode()
        {
            IngredientManager manager = new IngredientManager();
            var list = manager.LoadListIngredient();
            foreach (string item in list)
            {
                colValues.Add(item);
            }

        }

        /// <summary>btnSave_Click
        /// enregistrement des modifications
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            //on charge la liste des ingrédients associés
            List<string> ingredientValues = new List<string>();
            foreach (object o in lstIngredient.Items)
                ingredientValues.Add(o.ToString());
            
            PlatManager pm = new PlatManager();

            //si on modifie un plat
            if (m_idPlat >= -1)
                pm.Update(m_idPlat, txtNomPlat.Text, ingredientValues);
            else
                pm.add(txtNomPlat.Text, ingredientValues);

            loadGridView();
            LoadForm();
        }

        /// <summary> btnAddIngredient_Click
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddIngredient_Click(object sender, EventArgs e)
        {
            if (txtIngredientName.Text.Length > 0)
            {
                lstIngredient.Items.Add(txtIngredientName.Text);
                txtIngredientName.Clear();
            }
        }

        private void txtIngredientName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnAddIngredient_Click(sender, e);
        }

        /// <summary>bntDelete_Click
        /// suppression d'un element
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bntDelete_Click(object sender, EventArgs e)
        {
            IList faut 
                //il faut supprimer un ingredient
        }



    
    }
}
